package com.sibco.client.system;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.bstek.dorado.common.UserConfig;
import com.sibco.client.common.Tools;
import com.sibco.client.common.UrlMap;
import com.sibco.client.system.User;

public class UserLogonFilter implements Filter {
	private FilterConfig config = null;
  
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}
  
	public void destroy(  ) {
		config = null;
	}
    
	public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpReq = (HttpServletRequest) request;
		HttpServletResponse httpResp = (HttpServletResponse) response;
		
		String realUri=httpReq.getRequestURI();
		Object obj=httpReq.getSession().getAttribute("User");
		//System.out.println("DDDDDDDDDDDDDDDDDDDDDD1:"+realUri);
		String appRoot=Tools.toStr(UserConfig.getString("app_root"));
		if (realUri.equals(appRoot) || realUri.equals("/"+appRoot) || realUri.equals("/"+appRoot+"/") || realUri.equals(appRoot+"/")
			||realUri.indexOf("index.jsp")>=0 || realUri.indexOf("logout.jsp")>=0 || realUri.indexOf("dorado/")>=0
			||realUri.indexOf("forgetPassword.")>=0){
			chain.doFilter(request, response);
		}else{
			if(obj==null){
			   //System.out.println("DDDDDDDDDDDDDDDDDDDDDD2");
			   httpResp.sendRedirect(UrlMap.mapJspUrl("logout.jsp"));
			}else{
				chain.doFilter(request, response);
			}
		}		
	}

}
